<!-- 针车员工产量检核：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div style="background-color: white">
      <el-card shadow="never" :body-style="{ padding: '11px 20px' }" class="mt10">
        <div slot="header" class="f16">
          <span style="font-weight: 600">产量明细查询</span>
        </div>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          :summaryData="summaryData"
          summary
          :isLoading="isLoading"
          @selectList="selectTableList"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px' }">
        <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage } from 'element-plus'
import Synchronization from '@/utils/Synchronization'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
import SynchronizationLoginWork from '@/utils/SynchronizationLoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'RealCarEmployeeOutput'
})
</script>

<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入姓名',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工序名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入部门',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入序号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '单据类型',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: '1',
        optionValue: '针车正单',
        optionLabel: '针车正单'
      },
      {
        optionKey: '2',
        optionValue: '针车补单',
        optionLabel: '针车补单'
      }
    ]
  },
  {
    type: 'checkbox',
    checkboxLabel: '未检核',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已检核',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录检核'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录删除'
  }
]) as any
const tableHeader = ref([
  {
    label: '类型',
    prop: 'orderType'
  },
  {
    label: '日期',
    prop: 'slipDate'
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '订单量',
    prop: 'sum'
  },
  {
    label: '序号',
    prop: 'seqNo'
  },
  {
    backgroundColor: true,
    label: '工序名称',
    prop: 'processName',
    width: 120
  },
  {
    label: '部门',
    prop: 'deptName'
  },
  {
    label: '工号',
    prop: 'workId'
  },
  {
    label: '姓名',
    prop: 'workName'
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '输入员',
    prop: 'cfmUser'
  },
  {
    label: '检核标记',
    prop: 'cfmFlag'
  }
])
// 表一模块
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '打勾记录检核') {
    const res = (await SynchronizationLoginWork.post(`/costActivity/yieldDetailsCfm/${0}`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '取消检核') {
    const res = (await SynchronizationLoginWork.post(`/costActivity/yieldDetailsCfm/${1}`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('取消检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '打勾记录删除') {
    const res = (await SynchronizationLoginWork.post(`/costActivity/yieldDetailsCfm/${2}`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('删除成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 11,
    num: 0
  },
  {
    index: 13,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未检核')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已检核')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 2
  } else if (Audited.checked) {
    return 3
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await Synchronization.post<any, any>('/costActivity/getYieldDetails', {
    deptName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入部门'
    )?.inputValue,
    endDate: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '日期')
      ?.dateValue?.[1],
    orderType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '单据类型')
      ?.selectValue,
    processName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入工序名称'
    )?.inputValue,
    seqNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入序号'
    )?.inputValue,
    startDate: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '日期')
      ?.dateValue?.[0],
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
    )?.inputValue,
    workName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入姓名'
    )?.inputValue,
    workNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入工号'
    )?.inputValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    cfmFlag: cfmFlag() //	有无欠数(Y/N)	string	body	true
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    if (item.seqNo) {
      item.seqNo = Number(item.seqNo)
    }
    if (item.qty) {
      item.qty = Number(item.qty)
    }
    if (item.price) {
      item.price = Number(item.price)
    }
    if (item.amt) {
      item.amt = Number(item.amt)
    }
    if (item.orderType === 0 || item.orderType === 1) {
      item.orderType = item.orderType === 0 ? '针车正单' : '针车补单'
    }
    if (item.slipDate) {
      item.slipDate = item.slipDate.split(' ')[0]
    }
    if (item.cfmFlag) {
      item.backgroundColor = '#80FF00'
    }
    return item
  })
  console.log(tableData.value)

  total.value = tableDataList.total
  // currentRow()
  summaryData.value[0].num = tableDataList.qytCount.QTY
  summaryData.value[1].num = tableDataList.qytCount.AMT
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = JSON.parse(JSON.stringify(selectedList))
  selectedLists.value.forEach((item: any) => {
    item.orderType = item.orderType === '针车正单' ? 0 : 1
  })
}
</script>

<style lang="less" scoped></style>
